
package com.dps.omc.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.service.ITemplateShellService;
import com.dps.common.utils.tree.TreeBuilder;
import com.dps.common.utils.tree.TreeNode;
import com.dps.omc.model.TemplateType;
import com.dps.omc.service.ITemplateTypeService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class TemplateTypeAction extends AbstractAdminController<ITemplateTypeService>{

    @Autowired
    private ITemplateTypeService templateTypeService;
    @Autowired
    private ITemplateShellService templateShellService;
    
    @RequestMapping(value="/templatetype/list")
    public String list(TemplateType templateType, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(templateType); 	
    	BizData4Page<TemplateType> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("templateType", templateType);
    	return "/module/templatetype/list";
    }

    @RequestMapping(value="/templatetype/tree")
    public String tree(TemplateType templateType,ModelMap model,HttpServletRequest request,HttpServletResponse response){

//        List<TemplateType> types = templateTypeService.findAll();
//
//        List<TreeNode> treeNodes = new ArrayList<>();
//        for(TemplateType type:types)
//        {
//            TreeNode node = new TreeNode();
//            node.setId(type.getId());
//            node.setName(type.getTypeName());
//            node.setPid(type.getPid());
//            treeNodes.add(node);
//        }
//        TreeNode typeTree = TreeBuilder.buildTree(treeNodes);
//        model.addAttribute("typeTree", typeTree);
        return "/module/templatetype/tree";
    }

    @RequestMapping(value="/templatetype/ajaxtree")
    public String ajaxTree(TemplateType templateType,ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<TemplateType> types = templateTypeService.findAll();

        List<TreeNode> treeNodes = new ArrayList<>();
        for(TemplateType type:types)
        {
            TreeNode node = new TreeNode();
            node.setId(type.getId());
            node.setName(type.getTypeName());
            node.setPid(type.getPid());
            treeNodes.add(node);
        }
        TreeNode typeTree = TreeBuilder.buildTree(treeNodes);
        model.addAttribute("typeTree", typeTree);
        return "/module/templatetype/ajaxtree";
    }

    @RequestMapping(value="/templatetype/treejson")
    @ResponseBody
    public String treeJson(ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<TemplateType> types = templateTypeService.findAll();

        List<TreeNode> treeNodes = new ArrayList<>();
        for(TemplateType type:types)
        {
            TreeNode node = new TreeNode();
            node.setId(type.getId());
            node.setName(type.getTypeName());
            node.setPid(type.getPid());
            treeNodes.add(node);
        }
        TreeNode typeTree = TreeBuilder.buildTree(treeNodes);
        JSONObject obj = JSONObject.fromObject(typeTree);
        return obj.toString();
    }

    @RequestMapping(value="/templatetype/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	List<TemplateType> topTypes = templateTypeService.findList("pid", "root");
        model.addAttribute("templateType", new TemplateType());
        model.addAttribute("topTypes", topTypes);
        return "module/templatetype/edit";
    }    
    
    @RequestMapping(value="/templatetype/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TemplateType templateType = templateTypeService.fetch(id);
    	model.addAttribute("templateType", templateType);
        return "module/templatetype/edit";
    }    
    
    @RequestMapping(value="/templatetype/save")
    @ResponseBody
    public String save(HttpServletRequest request,TemplateType templateType){
		boolean isAddNew = StringUtils.isBlank(templateType.getId())?true:false;
		if(isAddNew)
		{
//			templateType.setId(IdGenerator.createNewId());
            String id = getId(templateType.getPid());
            templateType.setId(id);
            templateType.setNote(templateType.getTypeName());
			templateTypeService.add(templateType);
		}
		else
		{
			templateTypeService.update(templateType);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }

    private String  getId(String pid)
    {
        return templateTypeService.genId(pid);
    }
    
    @RequestMapping(value="/templatetype/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	templateTypeService.deleteByIds(ids);
        templateShellService.deleteByProperty("typeId",ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(TemplateType templateType)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("typeName", templateType.getTypeName());		
		conditions.put("note", templateType.getNote());		
		conditions.put("pid", templateType.getPid());		
		conditions.put("createrId", templateType.getCreaterId());		
		conditions.put("createrName", templateType.getCreaterName());		
		conditions.put("createDate", templateType.getCreateDate());		
    	return conditions;
    }

    @Override
    protected ITemplateTypeService getMainService() {
        return templateTypeService;
    }

    @Override
    protected String getMainObjName() {
        return "templatetype";
    }

    @Override
    protected String getViewTitle() {
        return "templatetype";
    }
}
